package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.CollageAdminRelations;
import com.xjc.business.entity.domain.Colleges;
import com.xjc.business.entity.domain.SchoolCollegeRelations;
import com.xjc.business.entity.po.schadmin.CollegePo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.schadmin.CollegeVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.CollegesMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【colleges(学院表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:31
*/
@Service
public class CollegesServiceImpl extends ServiceImpl<CollegesMapper, Colleges>
    implements CollegesService {

    @Autowired
    @Lazy
    SchoolCollegeRelationsService schoolCollegeRelationsService;

    @Autowired
    @Lazy
    CollageAdminRelationsService collageAdminRelationsService;

    @Autowired
    @Lazy
    CollegeAdminsService collegeAdminsService;

    @Autowired
    @Lazy
    SchoolsService schoolsService;

    /**
     * 获取学院分页列表
     *
     * @param pageSize    每页条数
     * @param currentPage 当前页码
     * @param schoolId    学校id
     * @param collegeName 学院名称
     * @return 列表数据
     */
    @Override
    public Result<PageVo<CollegeVo>> getCollegePage(Integer pageSize, Integer currentPage, Integer schoolId, String collegeName) {
        LambdaQueryWrapper<SchoolCollegeRelations> schoolCollegeRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolCollegeRelationsLambdaQueryWrapper.eq(SchoolCollegeRelations::getSchId, schoolId);

        List<SchoolCollegeRelations> schoolCollegeRelationsList = schoolCollegeRelationsService.list(schoolCollegeRelationsLambdaQueryWrapper);
        List<Integer> collectIdList = schoolCollegeRelationsList.stream().map(SchoolCollegeRelations::getColId).collect(Collectors.toList());

        PageVo<CollegeVo> pageVo = new PageVo<>();


        List<CollegeVo> collegeVoList = new ArrayList<>();

        if(!collectIdList.isEmpty()){
            LambdaQueryWrapper<Colleges> collegesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            collegesLambdaQueryWrapper.in(Colleges::getId, collectIdList);

            Page<Colleges> collegesPage = new Page<>(currentPage, pageSize);
            page(collegesPage, collegesLambdaQueryWrapper);
            List<Colleges> collegesPageRecords = collegesPage.getRecords();
            collegeVoList = collegesPageRecords.stream().map(colleges -> {
                CollegeVo collegeVo = new CollegeVo();
                collegeVo.setId(colleges.getId());
                collegeVo.setName(colleges.getName());
                collegeVo.setDesc(colleges.getDescs());
                collegeVo.setPic(colleges.getPic());
                collegeVo.setCreateTime(colleges.getCreateTime());
                LambdaQueryWrapper<CollageAdminRelations> collageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                collageAdminRelationsLambdaQueryWrapper.eq(CollageAdminRelations::getColId, colleges.getId());
                CollageAdminRelations collageAdminRelationsFromDB = collageAdminRelationsService.getOne(collageAdminRelationsLambdaQueryWrapper);
                if(collageAdminRelationsFromDB != null){
                    collegeVo.setAdminName(collegeAdminsService.getById(collageAdminRelationsFromDB.getColAdminId()).getUsername());
                }else {
                    collegeVo.setAdminName("未分配");
                }
                return collegeVo;
            }).collect(Collectors.toList());

            pageVo.setTotal(collegesPage.getTotal());
            pageVo.setPageCount(collegesPage.getPages());

        }else{
            pageVo.setTotal(0L);
            pageVo.setPageCount(0L);
        }

        pageVo.setData(collegeVoList);
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);

        return Result.okWithData(pageVo);
    }

    /**
     * 添加学院
     * @param collegePo 学院信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> addCollege(CollegePo collegePo) {
        Integer schoolId = collegePo.getSchoolId();

        Colleges colleges = new Colleges();
        colleges.setName(collegePo.getName());
        colleges.setDescs(collegePo.getDesc());
        colleges.setPic(collegePo.getPic());
        colleges.setCreateTime(new Date());
        colleges.setUpdateTime(new Date());

        save(colleges);

        SchoolCollegeRelations schoolCollegeRelations = new SchoolCollegeRelations();
        schoolCollegeRelations.setSchId(schoolId);
        schoolCollegeRelations.setColId(colleges.getId());
        schoolCollegeRelationsService.save(schoolCollegeRelations);

        return Result.okWithData("增加成功");
    }

    /**
     * 分配管理员
     *
     * @param adminId   管理员id
     * @param collegeId 学院id
     * @return 反馈
     */
    @Override
    public Result<String> associateAdmin(Integer adminId, Integer collegeId) {

        CollageAdminRelations collageAdminRelations = new CollageAdminRelations();
        collageAdminRelations.setColAdminId(adminId);
        collageAdminRelations.setColId(collegeId);
        collageAdminRelationsService.save(collageAdminRelations);
        return Result.okWithData("绑定成功");
    }

    /**
     * 获取学校-学院名称
     * @param id 学院id
     * @return 学院名称
     */
    @Override
    public Result<String> getCollegeName(Integer id) {
        String collegeName = getById(id).getName();
        LambdaQueryWrapper<SchoolCollegeRelations> schoolCollegeRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolCollegeRelationsLambdaQueryWrapper.eq(SchoolCollegeRelations::getColId, id);

        Integer schId = schoolCollegeRelationsService.getOne(schoolCollegeRelationsLambdaQueryWrapper).getSchId();

        String schoolName = schoolsService.getById(schId).getSchoolName();

        return Result.okWithData(schoolName+"-"+collegeName);
    }
}




